#pragma once

typedef void (*task_t)();

#define TaskNum 3

task_t tasks[TaskNum];

void Print()
{
    std::cout<<"This is Print task"<<std::endl;
}

void DownLoad()
{
    std::cout<<"This is DownLoad task"<<std::endl;
}

void Flush()
{
    std::cout<<"This is Flush task"<<std::endl;
}

void LoadTask()
{
    srand(time(nullptr) ^ getpid() ^ 17777);
    tasks[0]=Print;
    tasks[1]=DownLoad;
    tasks[2]=Flush;
}

void work()
{
    while(1)
    {
        int command = 0;
        int n = read(0,&command,sizeof(int));
        if(n == sizeof(int))
        {
            std::cout<<"pid: "<<getpid()<<"  handler task!"<<std::endl;
            tasks[command]();
        }       
        else if(n == 0)
        {
            std::cout<<"pid: "<<getpid()<<"  quit!"<<std::endl;
            break;
        }
    }
}

int SelectCommand()
{
    return rand() % TaskNum;
}